/*
 * ParseGenBank: A minimalistic event driven GenBank file parser in C++.
 *
 * Copyright (C) 2007 Andrew Miller
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include <exception>
#include <string>
#include <inttypes.h>

class EndOfSource
  : public std::exception
{
public:
  virtual ~EndOfSource() throw() {}
};

class IOException
{
public:
  IOException(const char* aMessage) throw();
  const char* what() const throw();

private:
  std::string mMessage;
};

class ParserException
{
public:
  ParserException(const char* aMessage) throw();
  const char* what() const throw();

  void appendLocation(uint32_t line, uint32_t col);

private:
  std::string mMessage;
};

class TextSource
{
public:
  virtual ~TextSource() {}
  virtual char nextChar() throw(EndOfSource) = 0;

  /* Don't need to free aBuf, but there is only one buffer per TextSource, so
   * you can't keep it past the next getBlock call. */
  virtual size_t getBlock(char** aBuf, uint32_t len) = 0;

  virtual size_t getOffset() = 0;
};

TextSource* NewBufferedFileSource(const char* aFilename, size_t aOffset = 0);

class GenBankSink
{
public:
  virtual ~GenBankSink() {};
  // Includes subkeywords too.
  virtual void OpenKeyword(const char* name, const char* value) = 0;
  virtual void CloseKeyword() = 0;
  virtual void OpenFeature(const char* name, const char* location) = 0;
  virtual void CloseFeature() = 0;
  virtual void Qualifier(const char* name, const char* value) = 0;
  virtual void CodingData(const char* data) = 0;
};

class GenBankParser
{
public:
  virtual ~GenBankParser() {};
  virtual void SetSource(TextSource* aSource) = 0;
  virtual void SetSink(GenBankSink* aSink) = 0;
  virtual void Parse() = 0;
  virtual void ParseCharacter(char c) = 0;
  virtual void EndOfFile() = 0;
  virtual size_t CountBufferedBytes() = 0;
};

GenBankParser* NewGenBankParser();
